<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>.NET Web Service Validation</title>
</head>
<body>

<h2>.NET Web Service Validation</h2>
<p>There was a great article on MSDN a while back that showed how to create a SOAP extension that would verify incoming requests against a schema, something .NET does not support out of the box (even in 2.0).&nbsp; Additionally they added quasi support for <a href="http://www.schematron.com">schematron</a> via Assert attributes.&nbsp; This allows for a very powerful input validation of web services.</p>
<p>This is a project to provide distribution, documentation, and some support for using this framework in .NET applications.&nbsp; It was driven by the need of customers and wanting to provide something better then a zip file of source one had to figure out the hard way.</p>
<p>The original article is available <a href="http://msdn.microsoft.com/msdnmag/issues/03/07/XMLSchemaValidation/">here</a>.</p>
<p><strong>Installation</strong></p>
<p>Download the installer and run.&nbsp; Easy :)</p>
<p><strong>Methods of Use</strong></p>
<p>There are two methods for using the validator.&nbsp; First you can force all web methods to be validated using the web.config file.&nbsp; Second you can mark methods using [Validation] attribute.</p>
<p><strong>Use</strong></p>
<p>Add a reference to SoapValidator.dll from your web service project.&nbsp; Modify your web.config to include the required settings.</p>
<h3>Attributes</h3>
<p>[<strong>Validation</strong>]</p>
<p>Mark web method for validation against schemas</p>
<p>[<strong>ValidationSchemaFolder</strong>(string relativeFolder)]</p>
<p>Used to add folders that contain schemas to load and cache.&nbsp; This attribute is only valid for classes.&nbsp; The relativeFolder parameter is relative to the vroot.</p>
<ul>
<li><em>relativeFolder</em> -- Folder of schemas to load and cache.&nbsp; Relative to the virtual root (vroot).</li></ul>
<p>[<strong>ValidationSchema</strong>(string schemaFile)]</p>
<p>Used to add schema files to load and cache.&nbsp; This attribute is only valid for classes.&nbsp; The schemaFile parameter is relative to the vroot.</p>
<ul>
<li><em>schemaFile</em> -- Schema file to load.&nbsp; Relative to the virtual root (vroot).</li></ul>
<p>[<strong>Assert</strong>(string rule)]</p>
<p>[<strong>Assert</strong>(string rule, string description)]</p>
<p>Used to add an XPath&nbsp;validation expression to a web method.&nbsp; The XPath expression must evaluate to true.</p>
<ul>
<li><em>rule</em> -- XPath validation expression.&nbsp; Must evaluate to true.</li>
<li><em>description</em> -- [optional] Description of assertion rule.</li></ul>
<p>[<strong>AssertNamespaceBinding</strong>(string prefix, string ns)]</p>
<p>Specifies namespace bindings used by assert xpath's.</p>
<ul>
<li><em>prefix</em> -- namespace prefix</li>
<li><em>ns</em> -- namespace to map to</li></ul>
<h3>Web.config Changes</h3>
<p>First two extensions must be registered by adding the following inside of the &lt;webServices&gt; node:</p>
<p></p>
<p></p>
<pre>&lt;soapExtensionReflectorTypes&gt;
  &lt;add type="SoapValidation.ValidationExtensionReflector, SoapValidation"/&gt;
&lt;/soapExtensionReflectorTypes&gt;
&lt;serviceDescriptionFormatExtensionTypes&gt;
  &lt;add type="SoapValidation.ValidationFormatExtension, SoapValidation"/&gt;
&lt;/serviceDescriptionFormatExtensionTypes&gt; 
</pre>
<p>Next, POST and GET protocols must be disabled by adding the following inside of the &lt;webServices&gt; node:</p>
<pre>&lt;protocols&gt;
  &lt;remove name="HttpPost" /&gt;
  &lt;remove name="HttpGet" /&gt;
&lt;/protocols&gt;
</pre>
<p>Finally, if you want to force all web methods to be validated with out using the [Validation] attribute add the following inside of the &lt;webServices&gt; node:</p>
<pre>&lt;soapExtensionTypes&gt;
  &lt;add type="SoapValidation.ValidationExtension, SoapValidation" priority="1" group="0" /&gt;
&lt;/soapExtensionTypes&gt;
</pre>
<p></p>
<p>&nbsp;</p>

</body>
</html>
